package com.kh.pajx.sop.dao.user.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.kh.pajx.sop.base.BaseDaoImpl;
import com.kh.pajx.sop.dao.user.ISysInsideUserDao;
import com.kh.pajx.sop.domain.SysInsideUser;

/**
 * 内部用户信息持久化操作实现类
 * @author:	azzcsimp
 * @Createdate:	2014年7月21日 上午9:43:53
 */
@Repository
@SuppressWarnings("unchecked")
public class SysInsideUserDaoImpl extends BaseDaoImpl<SysInsideUser> implements ISysInsideUserDao {
	
	// ==>1.ISysInsideUserDao重载方法
	
	/**
	 * @see com.kh.pajx.sop.dao.user.ISysInsideUserDao#findUserByGroup(java.lang.String, int)
	 */
	@Override
	public List<SysInsideUser> findUserByGroup(String groupCode, int userStatus) throws Exception{
		return getSession().createQuery(//
				"FROM SysInsideUser u WHERE u.isuGroupCode=? AND u.isuStatusFlag=? ") //
				.setParameter(0, groupCode) //
				.setParameter(1, userStatus) //
				.list();
	}
	
	// ==>1.IBaseDao重载方法
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	// =========================以下为需要整理或废弃方法===================================	
	
	@Override
	public List<SysInsideUser> getUserByPhone(String phone){
		return getSession().createQuery(//
				"FROM SysInsideUser u WHERE u.isuStatusFlag='1' AND u.isuPhone=?")
				.setParameter(0, phone)
				.list();
	}
	
	/**
	 * 根据登录名和密码获取用户信息
	 * @see com.kh.pajx.sop.dao.user.ISysInsideUserDao#getUserByAccountAndPassword(java.lang.String, java.lang.String)
	 */
	@Override
	public SysInsideUser getUserByAccountAndPassword(String account, String password){
		return (SysInsideUser) getSession().createQuery(//
				"FROM SysInsideUser u WHERE u.account=? AND u.password=?")//
				.setParameter(0, account)//
				.setParameter(1, password)//
				.uniqueResult(); 
	}
	
	@Override
	public List<SysInsideUser> getUserByGroupCode(String groupCode){
		return getSession().createQuery(//
				"FROM SysInsideUser u WHERE u.isuGroupCode = ?")
				.setParameter(0, groupCode)
				.list();
	}
	
	@Override
	public List<SysInsideUser> getUserByGroupCode(String groupCode, int flag){
		return getSession().createQuery(//
				"FROM SysInsideUser u WHERE u.isuGroupCode = ? and u.isuStatusFlag = ? ")
				.setParameter(0, groupCode)
				.setParameter(1, flag)
				.list();
	}
	
	@Override
	public List<SysInsideUser> getUserByRoleCode(String roleCode){
		return getSession().createQuery(//
				"FROM SysInsideUser u WHERE u.isuRoleCode = ?")
				.setParameter(0, roleCode)
				.list();
	}
	
	@Override
	public List<SysInsideUser> getUserByRoleCode(String roleCode, int flag){
		return getSession().createQuery(//
				"FROM SysInsideUser u WHERE u.isuRoleCode = ? and u.isuStatusFlag = ? ")
				.setParameter(0, roleCode)
				.setParameter(1, flag)
				.list();
	}
	@Override
	public List<SysInsideUser> getUserByRoleCode(String roleCode, int flag,String deptCode){
		return getSession().createQuery(//
				"FROM SysInsideUser u WHERE u.isuRoleCode = ? and u.deptCode=? and u.isuStatusFlag = ? ")
				.setParameter(0, roleCode)
				.setParameter(1, deptCode)
				.setParameter(2, flag)
				.list();
	}
	@Override
	public List<SysInsideUser> getUserByDeptCode(String deptCode){
		return getSession().createQuery(//
				"FROM SysInsideUser u WHERE u.deptCode = ?")
				.setParameter(0, deptCode)
				.list();
	}
	
	
	@Override
	public List<SysInsideUser> getUserByDeptCode(String deptCode, int flag){
		return getSession().createQuery(//
				"FROM SysInsideUser u WHERE u.deptCode = ? and u.isuStatusFlag = ? ")
				.setParameter(0, deptCode)
				.setParameter(1, flag)
				.list();
	}
	
	@Override
	public List<String> getUserIdListByDeptCode(String deptCode){
		return getSession().createQuery(//
				"SELECT u.isuId FROM SysInsideUser u WHERE u.deptCode = ?")
				.setParameter(0, deptCode)
				.list();
	}
	
	
	@Override
	public long countUserByRoleCode(String usrCode) {
		return (Long)getSession().createQuery("SELECT COUNT(*) FROM SysInsideUser a WHERE  a.isuRoleCode=?")
				.setParameter(0, usrCode).uniqueResult();
	}
	
	@Override
	public long countUserByDeptCode(String deptCode) {
		return (Long)getSession().createQuery("SELECT COUNT(*) FROM SysInsideUser a WHERE  a.deptCode=?")
				.setParameter(0, deptCode).uniqueResult();
	}
	
	@Override
	public List<String[]> getUserSimple(){
		return getSession().createSQLQuery(//
				"select isu.isu_id,isu.isu_name from sys_inside_user isu ")//
				.list();
	}

}
